package com.example.yigoumall.service;

import com.example.yigoumall.entity.Product;
import com.example.yigoumall.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ProductService {
    
    @Autowired
    private ProductMapper productMapper;
    
    public List<Product> findAll() {
        return productMapper.findAll();
    }
    
    public Product findById(Integer id) {
        return productMapper.findById(id);
    }
    
    public List<Product> searchByName(String name) {
        return productMapper.findByName(name);
    }
    
    public Product save(Product product) {
        if (product.getId() == null) {
            productMapper.insert(product);
        } else {
            productMapper.update(product);
        }
        return product;
    }
    
    public void deleteById(Integer id) {
        productMapper.deleteById(id);
    }
}